<?php
class wideout_system{
	var $db_host = 'localhost';
	var $db_user = 'root';
	var $db_pass = '';
	var $db_name = 'wideout_system';

	function wideout_system(){
		$this->connection = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
		@mysql_select_db($this->db_name);
	}
	
	// BEGIN ADD FLAGS //
	
	public function addPermissions($name,$description){
		$query = "INSERT INTO permissions (permission,description,date_created) VALUES ('$name','$description',NOW())";
		$result = @mysql_query($query) or die(mysql_error());
		
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function addTimeShifts($name,$description){
		$query = "INSERT INTO time_shifts (time_shift,description, date_created) VALUES ('$name','$description',NOW())";
		$result = @mysql_query($query) or die(mysql_error());
		
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function addPositions($name,$description){
		$query = "INSERT INTO positions (position_name,description, date_created) VALUES ('$name','$description',NOW())";
		$result = @mysql_query($query) or die(mysql_error());
		
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function addEmployeeStatus($name,$description){
		$query = "INSERT INTO emp_status (status_name,description, date_created) VALUES ('$name','$description',NOW())";
		$result = @mysql_query($query) or die(mysql_error());
		
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function addDepartments($name,$description){
		$query = "INSERT INTO departments (department,description, date_created) VALUES ('$name','$description',NOW())";
		$result = @mysql_query($query) or die(mysql_error());
		
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	// END ADD FLAGS //
	
	
	// BEGIN CREATE USER //
	
	public function createUser($username, $password){
		$pass = md5($password);
		$query = "INSERT INTO wo_users (username,password,date_created) VALUES ('$username','$pass',NOW())";
		$result = @mysql_query($query) or die(mysql_error());
		
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	// END CREATE USER //
	
	//BEGIN CREATE EMPLOYEE //
	
	public function createEmployee ($position, $user_id, $phones, $shift_id, $department_id, $firstname, $emails, $lastname, $birthdate, $sss, $tin, $philhealth, $acct_no, $emp_no, $emp_status, $created_by){
	
		$query = "INSERT INTO wo_user_profile (position,user_id,firstname,lastname,birthdate,sss,tin,philhealth,acct_no,emp_no,emp_status,created_by,date_created) VALUES ($position,$user_id,'$firstname','$lastname','$birthdate','$sss','$tin','$philhealth','$acct_no','$emp_no',$emp_status,$created_by,NOW())";
		$result = @mysql_query($query) or die(mysql_error());
		
		if($result){
			$query = "INSERT INTO wo_user_phones (user_id,phones,date_created) VALUES ($user_id, '$phones', NOW())";
			$result = @mysql_query($query) or die(mysql_error());
				if($result){
					$query = "INSERT INTO wo_user_emails (user_id,emails,date_created) VALUES ($user_id, '$emails', NOW())";
					$result = @mysql_query($query) or die(mysql_error());
					if($result){
						$query = "INSERT INTO wo_user_departments (user_id,department_id,date_created) VALUES ($user_id, $department_id, NOW())";
						$result = @mysql_query($query) or die(mysql_error());
						if($result){
							$query = "INSERT INTO wo_user_shifts (user_id,shift_id,date_created) VALUES ($user_id, $shift_id, NOW())";
							$result = @mysql_query($query) or die(mysql_error());
							if($result){
							return true;
							}
						}
					}
				}
	
		}else{
			return false;
		}
	}
	
	// END CREATE EMPLOYEE
	
	//BEGIN UPDATE EMPLOYEE //
	
	public function updateEmployee ($user_id, $position, $phones, $shift_id, $department_id, $firstname, $emails, $lastname, $birthdate, $sss, $tin, $philhealth, $acct_no, $emp_no, $emp_status,$last_update_by ){
	
		$query ="UPDATE wo_user_profile SET  position=$position, firstname='$firstname', lastname='$lastname', birthdate='$birthdate', sss='$sss', tin='$tin', philhealth='$philhealth', acct_no='$acct_no', emp_no='$emp_no', emp_status='$emp_status', last_update=NOW(), last_update_by='$last_update_by' WHERE user_id =$user_id";
		$result = @mysql_query($query) or die(mysql_error());
		
		if($result){
			$query ="UPDATE wo_user_phones SET phones='$phones' WHERE user_id =$user_id";
			$result = @mysql_query($query) or die(mysql_error());
				if($result){
					$query ="UPDATE wo_user_emails SET emails='$emails' WHERE user_id =$user_id";
					$result = @mysql_query($query) or die(mysql_error());
					if($result){
						$query ="UPDATE wo_user_departments SET department_id=$department_id WHERE user_id =$user_id";
						$result = @mysql_query($query) or die(mysql_error());
						if($result){
							$query = "UPDATE wo_user_shifts SET shift_id=$shift_id WHERE user_id =$user_id";
							$result = @mysql_query($query) or die(mysql_error());
							if($result){
							return true;
							}
						}
					}
				}
	
		}else{
			return false;
		}
	}
	
	// END UPDATES EMPLOYEE
	
	//START TIME IN
	public function timeIn ($user_id){
		$query = "INSERT INTO wo_users_timesheet (user_id,time_in) VALUES ('$user_id',NOW())";
		$result = @mysql_query($query) or die(mysql_error());
		
		if($result){
			return true;
		}else{
			return false;
		}
	}
	//END TIME IN
	
	//START TIME OUT
	public function timeOut($user_id){
		$total_hrs = "SELECT TIMEDIFF (NOW(), time_in)";
		$query = "UPDATE wo_users_timesheet SET time_out=NOW(), total_hrs='$total_hrs' WHERE user_id =$user_id";
		$result = @mysql_query($query) or die(mysql_error());
		if($result){
			return true;
		}else{
			return false;
		}
	}
	//END TIME OUT
}


?>